package org.abettor.myinj.ui.tabs;

import org.abettor.myinj.ui.box.InjectionPointKeyword;
import org.abettor.myinj.util.I18n;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationAdapter;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.OpenWindowListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * 检测注入点的界面
 * @author abettor
 *
 */
public class CheckInjectionPoint extends Composite
{
	private I18n i18n = new I18n();
	private Text txtUrl;
	private Browser browser;
	private Shell shell;
	private String keyword;

	/**
	 * 构造一个用于检测注入点的选项卡界面
	 * @see org.eclipse.swt.widgets.Composite#Composite(Composite, int)
	 */
	public CheckInjectionPoint(Composite parent, int style)
	{
		super(parent, style);
	}

	/**
	 * 将本选项卡及其所有控件显示出来
	 *
	 */
	public void show()
	{
		Rectangle rect = this.getClientArea();
		shell = this.getShell();

		//	URL标签
		Label labelUrl = new Label(this, SWT.NONE);
		labelUrl.setBounds(5, 5, 40, 20);
		labelUrl.setText(i18n.getString("main.tabs.check.labels.url"));

		//	URL地址栏
		txtUrl = new Text(this, SWT.BORDER);
		txtUrl.setText("http://www.abettor.org/");

		//	浏览按钮
		Button btnBrowse = new Button(this, SWT.BORDER);
		btnBrowse.setText(i18n.getString("main.tabs.check.buttons.browse.name"));
		btnBrowse.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(e.button == 1)
				{
					browser.stop();
					browser.setUrl(txtUrl.getText());
				}
			}
		}
		);

		//	后退按钮
		Button btnBack = new Button(this, SWT.BORDER);
		btnBack.setText(i18n.getString("main.tabs.check.buttons.back.name"));
		btnBack.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(e.button == 1)
				{
					browser.stop();
					browser.back();
				}
			}
		}
		);

		Button btnKeyword = new Button(this, SWT.BORDER);
		btnKeyword.setText(i18n.getString("main.tabs.check.buttons.keyword.name"));
		btnKeyword.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(e.button == 1)
				{
					//	设置关键字
					InjectionPointKeyword ipk = new InjectionPointKeyword(shell);
					ipk.setKeyword(keyword);
					ipk.show();
					if(ipk.isChanged())
					{
						keyword = ipk.getKeyword();
					}
				}
			}
		}
		);

		//	检测按钮
		Button btnCheck = new Button(this, SWT.BORDER);
		btnCheck.setText(i18n.getString("main.tabs.check.buttons.check.name"));
		btnCheck.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(e.button == 1)
				{
					//	TODO 开始检测注入点
				}
			}
		}
		);

		Button btnStop = new Button(this, SWT.BORDER);
		btnStop.setText(i18n.getString("main.tabs.check.buttons.stop.name"));
		btnStop.addMouseListener(new MouseAdapter()
		{
			public void mouseUp(MouseEvent e)
			{
				if(e.button == 1)
				{
					//	TODO 停止检测注入点
				}
			}
		}
		);

		//	控件重定位
		int txtWidth = rect.width - 10 - labelUrl.getSize().x - 50 - 50 - 50 - 50 - 50;
		txtUrl.setBounds(45, 5, txtWidth, 20);
		btnBrowse.setBounds(45 + txtWidth, 5, 50, 20);
		btnBack.setBounds(45 + txtWidth + 50, 5, 50, 20);
		btnKeyword.setBounds(45 + txtWidth + 50 + 50, 5, 50, 20);
		btnCheck.setBounds(45 + txtWidth + 50 + 50 + 50, 5, 50, 20);
		btnStop.setBounds(45 + txtWidth + 50 + 50 + 50 + 50, 5, 50, 20);

		//	浏览器对象
		browser = new Browser(this, SWT.NONE);
		browser.setBounds(5, 30, rect.width - 10, rect.height - 15 - labelUrl.getSize().y);
		browser.addLocationListener(new LocationAdapter()
		{
			public void changed(LocationEvent e)
			{
				txtUrl.setText(e.location);
			}
		}
		);
		browser.addOpenWindowListener(new OpenWindowListener()
		{
			public void open(WindowEvent e)
			{
				e.browser = browser;
			}
		}
		);
		browser.setUrl("http://www.abettor.org/");
	}
}
